package com.droidathletics.boundary.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MacrocycleDAOAdapter {
	private static final String DATABASE_NAME = "droidathleticsdb.db";
	private static final int DATABASE_VERSION = 1;

	private static final String MACROCYCLE_TABLE_NAME = "macrocycles";
    private static final String MACROCYCLE_TABLE_CREATE =
        "CREATE TABLE " + MACROCYCLE_TABLE_NAME + " (name TEXT, startdate TEXT, enddate TEXT, " +
        		"teamname TEXT, branch TEXT, category TEXT, competition TEXT, PRIMARY KEY(name))";
	
    private SQLiteDatabase database;
	private final Context context;
    private MacrocycleDBOpenHelper macrocycleHelper;
    
	public MacrocycleDAOAdapter(Context context) {
		this.context = context;
		macrocycleHelper = new MacrocycleDBOpenHelper(this.context);
	    this.database = macrocycleHelper.getWritableDatabase();
	}
	
	public static class MacrocycleDBOpenHelper extends SQLiteOpenHelper {
		
		public MacrocycleDBOpenHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(MACROCYCLE_TABLE_CREATE);	
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + MACROCYCLE_TABLE_NAME);
			onCreate(db);	
		}
	}

    public void close() {
        macrocycleHelper.close();
        database.close();
    }
    
    public void deleteTable()
    {
    	this.database.delete(MACROCYCLE_TABLE_NAME, null, null);
    }
    
    public long createMacrocycle(String name, 
   		 String dayStart, String monthStart, String yearStart,
		 String dayEnd, String monthEnd, String yearEnd,
		 String teamName, String branch, String category, String competition) {
        
    	long result = -1;
    	
    	ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("startdate", dayStart+"/"+monthStart+"/"+yearStart);
        values.put("enddate", dayEnd+"/"+monthEnd+"/"+yearEnd);
        values.put("teamname", teamName);
        values.put("branch", branch);
        values.put("category", category);
        values.put("competition", competition);
        
        try {
            result = database.insertOrThrow(MACROCYCLE_TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }

			
    public long updateMacrocycle(String oldName, String name, 
      		 String dayStart, String monthStart, String yearStart,
    		 String dayEnd, String monthEnd, String yearEnd,
    		 String teamName, String branch, String category, String competition) {
        
    	long result = -1;
    	
    	ContentValues values = new ContentValues();
 
        values.put("name", name);
        values.put("startdate", dayStart+"/"+monthStart+"/"+yearStart);
        values.put("enddate", dayEnd+"/"+monthEnd+"/"+yearEnd);
        values.put("teamname", teamName);
        values.put("branch", branch);
        values.put("category", category);
        values.put("competition", competition);
        
        try {
            result = database.update(MACROCYCLE_TABLE_NAME, values, 
            		      "name='"+name+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long deleteMacrocycle(String name){
        long result = -1;
        
    	try {
            result = database.delete(MACROCYCLE_TABLE_NAME, "name='"+name+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    	
    }

   public Cursor readMacrocycle(){
	   return database.query(MACROCYCLE_TABLE_NAME, new String[] {
				"name", "startdate", "enddate", "teamname",
				"branch", "category", "competition"}, null, null, null,
				null, null);
   
   }
   
   public Cursor searchMacrocycle(String par){
	   
	   String query = "name like " + "'" + par + "%'" +
	              " or teamname like " + "'" + par + "%'" +
			      " or category like " + "'" + par + "%'" +
	              " or competition like " + "'" + par + "%'" +
			      " or branch like " + "'" + par + "%'";
	   
	   return database.query(MACROCYCLE_TABLE_NAME, new String[] {
				"name", "startdate", "enddate", "teamname",
				"branch", "category", "competition"}, query, null, null,
				null, null);
   }
}
